datapath.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. static struct genl_family dp_packet_genl_family;
  59. static struct genl_family dp_flow_genl_family;
  60. static struct genl_family dp_datapath_genl_family;
  61. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  62. .name = OVS_FLOW_MCGROUP,
  63. };
  64. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  65. .name = OVS_DATAPATH_MCGROUP,
  66. };
  67. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  68. .name = OVS_VPORT_MCGROUP,
  69. };
  70. /* Check if need to build a reply message.
  71. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  72. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  73. unsigned int group)
  74. {
  75. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  76. genl_has_listeners(family, genl_info_net(info)->genl_sock,
  77. group);
  78. }
  79. static void ovs_notify(struct genl_family *family,
  80. struct sk_buff *skb, struct genl_info *info)
  81. {
  82. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  83. 0, info->nlhdr, GFP_KERNEL);
  84. }
  85. /**
  86. * DOC: Locking:
  87. *
  88. * All writes e.g. Writes to device state (add/remove datapath, port, set
  89. * operations on vports, etc.), Writes to other state (flow table
  90. * modifications, set miscellaneous datapath parameters, etc.) are protected
  91. * by ovs_lock.
  92. *
  93. * Reads are protected by RCU.
  94. *
  95. * There are a few special cases (mostly stats) that have their own
  96. * synchronization but they nest under all of above and don't interact with
  97. * each other.
  98. *
  99. * The RTNL lock nests inside ovs_mutex.
  100. */
  101. static DEFINE_MUTEX(ovs_mutex);
  102. void ovs_lock(void)
  103. {
  104. mutex_lock(&ovs_mutex);
  105. }
  106. void ovs_unlock(void)
  107. {
  108. mutex_unlock(&ovs_mutex);
  109. }
  110. #ifdef CONFIG_LOCKDEP
  111. int lockdep_ovsl_is_held(void)
  112. {
  113. if (debug_locks)
  114. return lockdep_is_held(&ovs_mutex);
  115. else
  116. return 1;
  117. }
  118. #endif
  119. static struct vport *new_vport(const struct vport_parms *);
  120. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  121. const struct dp_upcall_info *);
  122. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  123. const struct dp_upcall_info *);
  124. /* Must be called with rcu_read_lock or ovs_mutex. */
  125. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  126. {
  127. struct datapath *dp = NULL;
  128. struct net_device *dev;
  129. rcu_read_lock();
  130. dev = dev_get_by_index_rcu(net, dp_ifindex);
  131. if (dev) {
  132. struct vport *vport = ovs_internal_dev_get_vport(dev);
  133. if (vport)
  134. dp = vport->dp;
  135. }
  136. rcu_read_unlock();
  137. return dp;
  138. }
  139. /* Must be called with rcu_read_lock or ovs_mutex. */
  140. const char *ovs_dp_name(const struct datapath *dp)
  141. {
  142. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  143. return vport->ops->get_name(vport);
  144. }
  145. static int get_dpifindex(struct datapath *dp)
  146. {
  147. struct vport *local;
  148. int ifindex;
  149. rcu_read_lock();
  150. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  151. if (local)
  152. ifindex = netdev_vport_priv(local)->dev->ifindex;
  153. else
  154. ifindex = 0;
  155. rcu_read_unlock();
  156. return ifindex;
  157. }
  158. static void destroy_dp_rcu(struct rcu_head *rcu)
  159. {
  160. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  161. free_percpu(dp->stats_percpu);
  162. release_net(ovs_dp_get_net(dp));
  163. kfree(dp->ports);
  164. kfree(dp);
  165. }
  166. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  167. u16 port_no)
  168. {
  169. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  170. }
  171. /* Called with ovs_mutex or RCU read lock. */
  172. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  173. {
  174. struct vport *vport;
  175. struct hlist_head *head;
  176. head = vport_hash_bucket(dp, port_no);
  177. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  178. if (vport->port_no == port_no)
  179. return vport;
  180. }
  181. return NULL;
  182. }
  183. /* Called with ovs_mutex. */
  184. static struct vport *new_vport(const struct vport_parms *parms)
  185. {
  186. struct vport *vport;
  187. vport = ovs_vport_add(parms);
  188. if (!IS_ERR(vport)) {
  189. struct datapath *dp = parms->dp;
  190. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  191. hlist_add_head_rcu(&vport->dp_hash_node, head);
  192. }
  193. return vport;
  194. }
  195. void ovs_dp_detach_port(struct vport *p)
  196. {
  197. ASSERT_OVSL();
  198. /* First drop references to device. */
  199. hlist_del_rcu(&p->dp_hash_node);
  200. /* Then destroy it. */
  201. ovs_vport_del(p);
  202. }
  203. /* Must be called with rcu_read_lock. */
  204. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  205. {
  206. const struct vport *p = OVS_CB(skb)->input_vport;
  207. struct datapath *dp = p->dp;
  208. struct sw_flow *flow;
  209. struct dp_stats_percpu *stats;
  210. u64 *stats_counter;
  211. u32 n_mask_hit;
  212. stats = this_cpu_ptr(dp->stats_percpu);
  213. /* Look up flow. */
  214. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  215. if (unlikely(!flow)) {
  216. struct dp_upcall_info upcall;
  217. int error;
  218. upcall.cmd = OVS_PACKET_CMD_MISS;
  219. upcall.key = key;
  220. upcall.userdata = NULL;
  221. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  222. error = ovs_dp_upcall(dp, skb, &upcall);
  223. if (unlikely(error))
  224. kfree_skb(skb);
  225. else
  226. consume_skb(skb);
  227. stats_counter = &stats->n_missed;
  228. goto out;
  229. }
  230. OVS_CB(skb)->flow = flow;
  231. ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
  232. ovs_execute_actions(dp, skb, key);
  233. stats_counter = &stats->n_hit;
  234. out:
  235. /* Update datapath statistics. */
  236. u64_stats_update_begin(&stats->syncp);
  237. (*stats_counter)++;
  238. stats->n_mask_hit += n_mask_hit;
  239. u64_stats_update_end(&stats->syncp);
  240. }
  241. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  242. const struct dp_upcall_info *upcall_info)
  243. {
  244. struct dp_stats_percpu *stats;
  245. int err;
  246. if (upcall_info->portid == 0) {
  247. err = -ENOTCONN;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(dp, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(dp, skb, upcall_info);
  254. if (err)
  255. goto err;
  256. return 0;
  257. err:
  258. stats = this_cpu_ptr(dp->stats_percpu);
  259. u64_stats_update_begin(&stats->syncp);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->syncp);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  265. const struct dp_upcall_info *upcall_info)
  266. {
  267. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  268. struct dp_upcall_info later_info;
  269. struct sw_flow_key later_key;
  270. struct sk_buff *segs, *nskb;
  271. int err;
  272. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  273. if (IS_ERR(segs))
  274. return PTR_ERR(segs);
  275. if (segs == NULL)
  276. return -EINVAL;
  277. /* Queue all of the segments. */
  278. skb = segs;
  279. do {
  280. err = queue_userspace_packet(dp, skb, upcall_info);
  281. if (err)
  282. break;
  283. if (skb == segs && gso_type & SKB_GSO_UDP) {
  284. /* The initial flow key extracted by ovs_flow_extract()
  285. * in this case is for a first fragment, so we need to
  286. * properly mark later fragments.
  287. */
  288. later_key = *upcall_info->key;
  289. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  290. later_info = *upcall_info;
  291. later_info.key = &later_key;
  292. upcall_info = &later_info;
  293. }
  294. } while ((skb = skb->next));
  295. /* Free all of the segments. */
  296. skb = segs;
  297. do {
  298. nskb = skb->next;
  299. if (err)
  300. kfree_skb(skb);
  301. else
  302. consume_skb(skb);
  303. } while ((skb = nskb));
  304. return err;
  305. }
  306. static size_t key_attr_size(void)
  307. {
  308. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  309. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  310. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  311. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  312. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  313. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  314. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  315. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  316. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  317. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  318. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  319. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  321. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  322. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  323. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  324. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  325. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  326. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  327. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  328. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  329. }
  330. static size_t upcall_msg_size(const struct nlattr *userdata,
  331. unsigned int hdrlen)
  332. {
  333. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  334. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  335. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  336. /* OVS_PACKET_ATTR_USERDATA */
  337. if (userdata)
  338. size += NLA_ALIGN(userdata->nla_len);
  339. return size;
  340. }
  341. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  342. const struct dp_upcall_info *upcall_info)
  343. {
  344. struct ovs_header *upcall;
  345. struct sk_buff *nskb = NULL;
  346. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  347. struct nlattr *nla;
  348. struct genl_info info = {
  349. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  350. .snd_portid = upcall_info->portid,
  351. };
  352. size_t len;
  353. unsigned int hlen;
  354. int err, dp_ifindex;
  355. dp_ifindex = get_dpifindex(dp);
  356. if (!dp_ifindex)
  357. return -ENODEV;
  358. if (vlan_tx_tag_present(skb)) {
  359. nskb = skb_clone(skb, GFP_ATOMIC);
  360. if (!nskb)
  361. return -ENOMEM;
  362. nskb = __vlan_hwaccel_push_inside(nskb);
  363. if (!nskb)
  364. return -ENOMEM;
  365. skb = nskb;
  366. }
  367. if (nla_attr_size(skb->len) > USHRT_MAX) {
  368. err = -EFBIG;
  369. goto out;
  370. }
  371. /* Complete checksum if needed */
  372. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  373. (err = skb_checksum_help(skb)))
  374. goto out;
  375. /* Older versions of OVS user space enforce alignment of the last
  376. * Netlink attribute to NLA_ALIGNTO which would require extensive
  377. * padding logic. Only perform zerocopy if padding is not required.
  378. */
  379. if (dp->user_features & OVS_DP_F_UNALIGNED)
  380. hlen = skb_zerocopy_headlen(skb);
  381. else
  382. hlen = skb->len;
  383. len = upcall_msg_size(upcall_info->userdata, hlen);
  384. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  385. if (!user_skb) {
  386. err = -ENOMEM;
  387. goto out;
  388. }
  389. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  390. 0, upcall_info->cmd);
  391. upcall->dp_ifindex = dp_ifindex;
  392. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  393. err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  394. BUG_ON(err);
  395. nla_nest_end(user_skb, nla);
  396. if (upcall_info->userdata)
  397. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  398. nla_len(upcall_info->userdata),
  399. nla_data(upcall_info->userdata));
  400. /* Only reserve room for attribute header, packet data is added
  401. * in skb_zerocopy() */
  402. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  403. err = -ENOBUFS;
  404. goto out;
  405. }
  406. nla->nla_len = nla_attr_size(skb->len);
  407. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  408. if (err)
  409. goto out;
  410. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  411. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  412. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  413. if (plen > 0)
  414. memset(skb_put(user_skb, plen), 0, plen);
  415. }
  416. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  417. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  418. user_skb = NULL;
  419. out:
  420. if (err)
  421. skb_tx_error(skb);
  422. kfree_skb(user_skb);
  423. kfree_skb(nskb);
  424. return err;
  425. }
  426. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  427. {
  428. struct ovs_header *ovs_header = info->userhdr;
  429. struct nlattr **a = info->attrs;
  430. struct sw_flow_actions *acts;
  431. struct sk_buff *packet;
  432. struct sw_flow *flow;
  433. struct datapath *dp;
  434. struct ethhdr *eth;
  435. struct vport *input_vport;
  436. int len;
  437. int err;
  438. err = -EINVAL;
  439. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  440. !a[OVS_PACKET_ATTR_ACTIONS])
  441. goto err;
  442. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  443. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  444. err = -ENOMEM;
  445. if (!packet)
  446. goto err;
  447. skb_reserve(packet, NET_IP_ALIGN);
  448. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  449. skb_reset_mac_header(packet);
  450. eth = eth_hdr(packet);
  451. /* Normally, setting the skb 'protocol' field would be handled by a
  452. * call to eth_type_trans(), but it assumes there's a sending
  453. * device, which we may not have. */
  454. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  455. packet->protocol = eth->h_proto;
  456. else
  457. packet->protocol = htons(ETH_P_802_2);
  458. /* Build an sw_flow for sending this packet. */
  459. flow = ovs_flow_alloc();
  460. err = PTR_ERR(flow);
  461. if (IS_ERR(flow))
  462. goto err_kfree_skb;
  463. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  464. &flow->key);
  465. if (err)
  466. goto err_flow_free;
  467. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  468. err = PTR_ERR(acts);
  469. if (IS_ERR(acts))
  470. goto err_flow_free;
  471. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  472. &flow->key, 0, &acts);
  473. if (err)
  474. goto err_flow_free;
  475. rcu_assign_pointer(flow->sf_acts, acts);
  476. OVS_CB(packet)->egress_tun_info = NULL;
  477. OVS_CB(packet)->flow = flow;
  478. packet->priority = flow->key.phy.priority;
  479. packet->mark = flow->key.phy.skb_mark;
  480. rcu_read_lock();
  481. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  482. err = -ENODEV;
  483. if (!dp)
  484. goto err_unlock;
  485. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  486. if (!input_vport)
  487. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  488. if (!input_vport)
  489. goto err_unlock;
  490. OVS_CB(packet)->input_vport = input_vport;
  491. local_bh_disable();
  492. err = ovs_execute_actions(dp, packet, &flow->key);
  493. local_bh_enable();
  494. rcu_read_unlock();
  495. ovs_flow_free(flow, false);
  496. return err;
  497. err_unlock:
  498. rcu_read_unlock();
  499. err_flow_free:
  500. ovs_flow_free(flow, false);
  501. err_kfree_skb:
  502. kfree_skb(packet);
  503. err:
  504. return err;
  505. }
  506. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  507. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  508. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  509. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  510. };
  511. static const struct genl_ops dp_packet_genl_ops[] = {
  512. { .cmd = OVS_PACKET_CMD_EXECUTE,
  513. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  514. .policy = packet_policy,
  515. .doit = ovs_packet_cmd_execute
  516. }
  517. };
  518. static struct genl_family dp_packet_genl_family = {
  519. .id = GENL_ID_GENERATE,
  520. .hdrsize = sizeof(struct ovs_header),
  521. .name = OVS_PACKET_FAMILY,
  522. .version = OVS_PACKET_VERSION,
  523. .maxattr = OVS_PACKET_ATTR_MAX,
  524. .netnsok = true,
  525. .parallel_ops = true,
  526. .ops = dp_packet_genl_ops,
  527. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  528. };
  529. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  530. struct ovs_dp_megaflow_stats *mega_stats)
  531. {
  532. int i;
  533. memset(mega_stats, 0, sizeof(*mega_stats));
  534. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  535. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  536. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  537. for_each_possible_cpu(i) {
  538. const struct dp_stats_percpu *percpu_stats;
  539. struct dp_stats_percpu local_stats;
  540. unsigned int start;
  541. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  542. do {
  543. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  544. local_stats = *percpu_stats;
  545. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  546. stats->n_hit += local_stats.n_hit;
  547. stats->n_missed += local_stats.n_missed;
  548. stats->n_lost += local_stats.n_lost;
  549. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  550. }
  551. }
  552. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  553. {
  554. return NLMSG_ALIGN(sizeof(struct ovs_header))
  555. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  556. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  557. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  558. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  559. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  560. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  561. }
  562. /* Called with ovs_mutex or RCU read lock. */
  563. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  564. struct sk_buff *skb, u32 portid,
  565. u32 seq, u32 flags, u8 cmd)
  566. {
  567. const int skb_orig_len = skb->len;
  568. struct nlattr *start;
  569. struct ovs_flow_stats stats;
  570. __be16 tcp_flags;
  571. unsigned long used;
  572. struct ovs_header *ovs_header;
  573. struct nlattr *nla;
  574. int err;
  575. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  576. if (!ovs_header)
  577. return -EMSGSIZE;
  578. ovs_header->dp_ifindex = dp_ifindex;
  579. /* Fill flow key. */
  580. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  581. if (!nla)
  582. goto nla_put_failure;
  583. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  584. if (err)
  585. goto error;
  586. nla_nest_end(skb, nla);
  587. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  588. if (!nla)
  589. goto nla_put_failure;
  590. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  591. if (err)
  592. goto error;
  593. nla_nest_end(skb, nla);
  594. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  595. if (used &&
  596. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  597. goto nla_put_failure;
  598. if (stats.n_packets &&
  599. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  600. goto nla_put_failure;
  601. if ((u8)ntohs(tcp_flags) &&
  602. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  603. goto nla_put_failure;
  604. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  605. * this is the first flow to be dumped into 'skb'. This is unusual for
  606. * Netlink but individual action lists can be longer than
  607. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  608. * The userspace caller can always fetch the actions separately if it
  609. * really wants them. (Most userspace callers in fact don't care.)
  610. *
  611. * This can only fail for dump operations because the skb is always
  612. * properly sized for single flows.
  613. */
  614. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  615. if (start) {
  616. const struct sw_flow_actions *sf_acts;
  617. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  618. err = ovs_nla_put_actions(sf_acts->actions,
  619. sf_acts->actions_len, skb);
  620. if (!err)
  621. nla_nest_end(skb, start);
  622. else {
  623. if (skb_orig_len)
  624. goto error;
  625. nla_nest_cancel(skb, start);
  626. }
  627. } else if (skb_orig_len)
  628. goto nla_put_failure;
  629. return genlmsg_end(skb, ovs_header);
  630. nla_put_failure:
  631. err = -EMSGSIZE;
  632. error:
  633. genlmsg_cancel(skb, ovs_header);
  634. return err;
  635. }
  636. /* May not be called with RCU read lock. */
  637. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  638. struct genl_info *info,
  639. bool always)
  640. {
  641. struct sk_buff *skb;
  642. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  643. return NULL;
  644. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  645. if (!skb)
  646. return ERR_PTR(-ENOMEM);
  647. return skb;
  648. }
  649. /* Called with ovs_mutex. */
  650. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  651. int dp_ifindex,
  652. struct genl_info *info, u8 cmd,
  653. bool always)
  654. {
  655. struct sk_buff *skb;
  656. int retval;
  657. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  658. always);
  659. if (IS_ERR_OR_NULL(skb))
  660. return skb;
  661. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  662. info->snd_portid, info->snd_seq, 0,
  663. cmd);
  664. BUG_ON(retval < 0);
  665. return skb;
  666. }
  667. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  668. {
  669. struct nlattr **a = info->attrs;
  670. struct ovs_header *ovs_header = info->userhdr;
  671. struct sw_flow *flow, *new_flow;
  672. struct sw_flow_mask mask;
  673. struct sk_buff *reply;
  674. struct datapath *dp;
  675. struct sw_flow_actions *acts;
  676. struct sw_flow_match match;
  677. int error;
  678. /* Must have key and actions. */
  679. error = -EINVAL;
  680. if (!a[OVS_FLOW_ATTR_KEY])
  681. goto error;
  682. if (!a[OVS_FLOW_ATTR_ACTIONS])
  683. goto error;
  684. /* Most of the time we need to allocate a new flow, do it before
  685. * locking.
  686. */
  687. new_flow = ovs_flow_alloc();
  688. if (IS_ERR(new_flow)) {
  689. error = PTR_ERR(new_flow);
  690. goto error;
  691. }
  692. /* Extract key. */
  693. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  694. error = ovs_nla_get_match(&match,
  695. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  696. if (error)
  697. goto err_kfree_flow;
  698. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  699. /* Validate actions. */
  700. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  701. error = PTR_ERR(acts);
  702. if (IS_ERR(acts))
  703. goto err_kfree_flow;
  704. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  705. 0, &acts);
  706. if (error) {
  707. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  708. goto err_kfree_acts;
  709. }
  710. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  711. if (IS_ERR(reply)) {
  712. error = PTR_ERR(reply);
  713. goto err_kfree_acts;
  714. }
  715. ovs_lock();
  716. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  717. if (unlikely(!dp)) {
  718. error = -ENODEV;
  719. goto err_unlock_ovs;
  720. }
  721. /* Check if this is a duplicate flow */
  722. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  723. if (likely(!flow)) {
  724. rcu_assign_pointer(new_flow->sf_acts, acts);
  725. /* Put flow in bucket. */
  726. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  727. if (unlikely(error)) {
  728. acts = NULL;
  729. goto err_unlock_ovs;
  730. }
  731. if (unlikely(reply)) {
  732. error = ovs_flow_cmd_fill_info(new_flow,
  733. ovs_header->dp_ifindex,
  734. reply, info->snd_portid,
  735. info->snd_seq, 0,
  736. OVS_FLOW_CMD_NEW);
  737. BUG_ON(error < 0);
  738. }
  739. ovs_unlock();
  740. } else {
  741. struct sw_flow_actions *old_acts;
  742. /* Bail out if we're not allowed to modify an existing flow.
  743. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  744. * because Generic Netlink treats the latter as a dump
  745. * request. We also accept NLM_F_EXCL in case that bug ever
  746. * gets fixed.
  747. */
  748. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  749. | NLM_F_EXCL))) {
  750. error = -EEXIST;
  751. goto err_unlock_ovs;
  752. }
  753. /* The unmasked key has to be the same for flow updates. */
  754. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  755. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  756. if (!flow) {
  757. error = -ENOENT;
  758. goto err_unlock_ovs;
  759. }
  760. }
  761. /* Update actions. */
  762. old_acts = ovsl_dereference(flow->sf_acts);
  763. rcu_assign_pointer(flow->sf_acts, acts);
  764. if (unlikely(reply)) {
  765. error = ovs_flow_cmd_fill_info(flow,
  766. ovs_header->dp_ifindex,
  767. reply, info->snd_portid,
  768. info->snd_seq, 0,
  769. OVS_FLOW_CMD_NEW);
  770. BUG_ON(error < 0);
  771. }
  772. ovs_unlock();
  773. ovs_nla_free_flow_actions(old_acts);
  774. ovs_flow_free(new_flow, false);
  775. }
  776. if (reply)
  777. ovs_notify(&dp_flow_genl_family, reply, info);
  778. return 0;
  779. err_unlock_ovs:
  780. ovs_unlock();
  781. kfree_skb(reply);
  782. err_kfree_acts:
  783. kfree(acts);
  784. err_kfree_flow:
  785. ovs_flow_free(new_flow, false);
  786. error:
  787. return error;
  788. }
  789. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  790. const struct sw_flow_key *key,
  791. const struct sw_flow_mask *mask)
  792. {
  793. struct sw_flow_actions *acts;
  794. struct sw_flow_key masked_key;
  795. int error;
  796. acts = ovs_nla_alloc_flow_actions(nla_len(a));
  797. if (IS_ERR(acts))
  798. return acts;
  799. ovs_flow_mask_key(&masked_key, key, mask);
  800. error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
  801. if (error) {
  802. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  803. kfree(acts);
  804. return ERR_PTR(error);
  805. }
  806. return acts;
  807. }
  808. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  809. {
  810. struct nlattr **a = info->attrs;
  811. struct ovs_header *ovs_header = info->userhdr;
  812. struct sw_flow_key key;
  813. struct sw_flow *flow;
  814. struct sw_flow_mask mask;
  815. struct sk_buff *reply = NULL;
  816. struct datapath *dp;
  817. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  818. struct sw_flow_match match;
  819. int error;
  820. /* Extract key. */
  821. error = -EINVAL;
  822. if (!a[OVS_FLOW_ATTR_KEY])
  823. goto error;
  824. ovs_match_init(&match, &key, &mask);
  825. error = ovs_nla_get_match(&match,
  826. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  827. if (error)
  828. goto error;
  829. /* Validate actions. */
  830. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  831. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
  832. if (IS_ERR(acts)) {
  833. error = PTR_ERR(acts);
  834. goto error;
  835. }
  836. }
  837. /* Can allocate before locking if have acts. */
  838. if (acts) {
  839. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  840. if (IS_ERR(reply)) {
  841. error = PTR_ERR(reply);
  842. goto err_kfree_acts;
  843. }
  844. }
  845. ovs_lock();
  846. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  847. if (unlikely(!dp)) {
  848. error = -ENODEV;
  849. goto err_unlock_ovs;
  850. }
  851. /* Check that the flow exists. */
  852. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  853. if (unlikely(!flow)) {
  854. error = -ENOENT;
  855. goto err_unlock_ovs;
  856. }
  857. /* Update actions, if present. */
  858. if (likely(acts)) {
  859. old_acts = ovsl_dereference(flow->sf_acts);
  860. rcu_assign_pointer(flow->sf_acts, acts);
  861. if (unlikely(reply)) {
  862. error = ovs_flow_cmd_fill_info(flow,
  863. ovs_header->dp_ifindex,
  864. reply, info->snd_portid,
  865. info->snd_seq, 0,
  866. OVS_FLOW_CMD_NEW);
  867. BUG_ON(error < 0);
  868. }
  869. } else {
  870. /* Could not alloc without acts before locking. */
  871. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  872. info, OVS_FLOW_CMD_NEW, false);
  873. if (unlikely(IS_ERR(reply))) {
  874. error = PTR_ERR(reply);
  875. goto err_unlock_ovs;
  876. }
  877. }
  878. /* Clear stats. */
  879. if (a[OVS_FLOW_ATTR_CLEAR])
  880. ovs_flow_stats_clear(flow);
  881. ovs_unlock();
  882. if (reply)
  883. ovs_notify(&dp_flow_genl_family, reply, info);
  884. if (old_acts)
  885. ovs_nla_free_flow_actions(old_acts);
  886. return 0;
  887. err_unlock_ovs:
  888. ovs_unlock();
  889. kfree_skb(reply);
  890. err_kfree_acts:
  891. kfree(acts);
  892. error:
  893. return error;
  894. }
  895. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  896. {
  897. struct nlattr **a = info->attrs;
  898. struct ovs_header *ovs_header = info->userhdr;
  899. struct sw_flow_key key;
  900. struct sk_buff *reply;
  901. struct sw_flow *flow;
  902. struct datapath *dp;
  903. struct sw_flow_match match;
  904. int err;
  905. if (!a[OVS_FLOW_ATTR_KEY]) {
  906. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  907. return -EINVAL;
  908. }
  909. ovs_match_init(&match, &key, NULL);
  910. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  911. if (err)
  912. return err;
  913. ovs_lock();
  914. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  915. if (!dp) {
  916. err = -ENODEV;
  917. goto unlock;
  918. }
  919. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  920. if (!flow) {
  921. err = -ENOENT;
  922. goto unlock;
  923. }
  924. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  925. OVS_FLOW_CMD_NEW, true);
  926. if (IS_ERR(reply)) {
  927. err = PTR_ERR(reply);
  928. goto unlock;
  929. }
  930. ovs_unlock();
  931. return genlmsg_reply(reply, info);
  932. unlock:
  933. ovs_unlock();
  934. return err;
  935. }
  936. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  937. {
  938. struct nlattr **a = info->attrs;
  939. struct ovs_header *ovs_header = info->userhdr;
  940. struct sw_flow_key key;
  941. struct sk_buff *reply;
  942. struct sw_flow *flow;
  943. struct datapath *dp;
  944. struct sw_flow_match match;
  945. int err;
  946. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  947. ovs_match_init(&match, &key, NULL);
  948. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  949. if (unlikely(err))
  950. return err;
  951. }
  952. ovs_lock();
  953. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  954. if (unlikely(!dp)) {
  955. err = -ENODEV;
  956. goto unlock;
  957. }
  958. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  959. err = ovs_flow_tbl_flush(&dp->table);
  960. goto unlock;
  961. }
  962. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  963. if (unlikely(!flow)) {
  964. err = -ENOENT;
  965. goto unlock;
  966. }
  967. ovs_flow_tbl_remove(&dp->table, flow);
  968. ovs_unlock();
  969. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  970. info, false);
  971. if (likely(reply)) {
  972. if (likely(!IS_ERR(reply))) {
  973. rcu_read_lock(); /*To keep RCU checker happy. */
  974. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  975. reply, info->snd_portid,
  976. info->snd_seq, 0,
  977. OVS_FLOW_CMD_DEL);
  978. rcu_read_unlock();
  979. BUG_ON(err < 0);
  980. ovs_notify(&dp_flow_genl_family, reply, info);
  981. } else {
  982. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  983. }
  984. }
  985. ovs_flow_free(flow, true);
  986. return 0;
  987. unlock:
  988. ovs_unlock();
  989. return err;
  990. }
  991. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  992. {
  993. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  994. struct table_instance *ti;
  995. struct datapath *dp;
  996. rcu_read_lock();
  997. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  998. if (!dp) {
  999. rcu_read_unlock();
  1000. return -ENODEV;
  1001. }
  1002. ti = rcu_dereference(dp->table.ti);
  1003. for (;;) {
  1004. struct sw_flow *flow;
  1005. u32 bucket, obj;
  1006. bucket = cb->args[0];
  1007. obj = cb->args[1];
  1008. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1009. if (!flow)
  1010. break;
  1011. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1012. NETLINK_CB(cb->skb).portid,
  1013. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1014. OVS_FLOW_CMD_NEW) < 0)
  1015. break;
  1016. cb->args[0] = bucket;
  1017. cb->args[1] = obj;
  1018. }
  1019. rcu_read_unlock();
  1020. return skb->len;
  1021. }
  1022. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1023. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1024. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1025. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1026. };
  1027. static const struct genl_ops dp_flow_genl_ops[] = {
  1028. { .cmd = OVS_FLOW_CMD_NEW,
  1029. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1030. .policy = flow_policy,
  1031. .doit = ovs_flow_cmd_new
  1032. },
  1033. { .cmd = OVS_FLOW_CMD_DEL,
  1034. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1035. .policy = flow_policy,
  1036. .doit = ovs_flow_cmd_del
  1037. },
  1038. { .cmd = OVS_FLOW_CMD_GET,
  1039. .flags = 0, /* OK for unprivileged users. */
  1040. .policy = flow_policy,
  1041. .doit = ovs_flow_cmd_get,
  1042. .dumpit = ovs_flow_cmd_dump
  1043. },
  1044. { .cmd = OVS_FLOW_CMD_SET,
  1045. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1046. .policy = flow_policy,
  1047. .doit = ovs_flow_cmd_set,
  1048. },
  1049. };
  1050. static struct genl_family dp_flow_genl_family = {
  1051. .id = GENL_ID_GENERATE,
  1052. .hdrsize = sizeof(struct ovs_header),
  1053. .name = OVS_FLOW_FAMILY,
  1054. .version = OVS_FLOW_VERSION,
  1055. .maxattr = OVS_FLOW_ATTR_MAX,
  1056. .netnsok = true,
  1057. .parallel_ops = true,
  1058. .ops = dp_flow_genl_ops,
  1059. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1060. .mcgrps = &ovs_dp_flow_multicast_group,
  1061. .n_mcgrps = 1,
  1062. };
  1063. static size_t ovs_dp_cmd_msg_size(void)
  1064. {
  1065. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1066. msgsize += nla_total_size(IFNAMSIZ);
  1067. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1068. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1069. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1070. return msgsize;
  1071. }
  1072. /* Called with ovs_mutex. */
  1073. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1074. u32 portid, u32 seq, u32 flags, u8 cmd)
  1075. {
  1076. struct ovs_header *ovs_header;
  1077. struct ovs_dp_stats dp_stats;
  1078. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1079. int err;
  1080. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1081. flags, cmd);
  1082. if (!ovs_header)
  1083. goto error;
  1084. ovs_header->dp_ifindex = get_dpifindex(dp);
  1085. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1086. if (err)
  1087. goto nla_put_failure;
  1088. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1089. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1090. &dp_stats))
  1091. goto nla_put_failure;
  1092. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1093. sizeof(struct ovs_dp_megaflow_stats),
  1094. &dp_megaflow_stats))
  1095. goto nla_put_failure;
  1096. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1097. goto nla_put_failure;
  1098. return genlmsg_end(skb, ovs_header);
  1099. nla_put_failure:
  1100. genlmsg_cancel(skb, ovs_header);
  1101. error:
  1102. return -EMSGSIZE;
  1103. }
  1104. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1105. {
  1106. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1107. }
  1108. /* Called with rcu_read_lock or ovs_mutex. */
  1109. static struct datapath *lookup_datapath(struct net *net,
  1110. struct ovs_header *ovs_header,
  1111. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1112. {
  1113. struct datapath *dp;
  1114. if (!a[OVS_DP_ATTR_NAME])
  1115. dp = get_dp(net, ovs_header->dp_ifindex);
  1116. else {
  1117. struct vport *vport;
  1118. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1119. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1120. }
  1121. return dp ? dp : ERR_PTR(-ENODEV);
  1122. }
  1123. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1124. {
  1125. struct datapath *dp;
  1126. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1127. if (IS_ERR(dp))
  1128. return;
  1129. WARN(dp->user_features, "Dropping previously announced user features\n");
  1130. dp->user_features = 0;
  1131. }
  1132. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1133. {
  1134. if (a[OVS_DP_ATTR_USER_FEATURES])
  1135. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1136. }
  1137. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1138. {
  1139. struct nlattr **a = info->attrs;
  1140. struct vport_parms parms;
  1141. struct sk_buff *reply;
  1142. struct datapath *dp;
  1143. struct vport *vport;
  1144. struct ovs_net *ovs_net;
  1145. int err, i;
  1146. err = -EINVAL;
  1147. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1148. goto err;
  1149. reply = ovs_dp_cmd_alloc_info(info);
  1150. if (!reply)
  1151. return -ENOMEM;
  1152. err = -ENOMEM;
  1153. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1154. if (dp == NULL)
  1155. goto err_free_reply;
  1156. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1157. /* Allocate table. */
  1158. err = ovs_flow_tbl_init(&dp->table);
  1159. if (err)
  1160. goto err_free_dp;
  1161. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1162. if (!dp->stats_percpu) {
  1163. err = -ENOMEM;
  1164. goto err_destroy_table;
  1165. }
  1166. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1167. GFP_KERNEL);
  1168. if (!dp->ports) {
  1169. err = -ENOMEM;
  1170. goto err_destroy_percpu;
  1171. }
  1172. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1173. INIT_HLIST_HEAD(&dp->ports[i]);
  1174. /* Set up our datapath device. */
  1175. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1176. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1177. parms.options = NULL;
  1178. parms.dp = dp;
  1179. parms.port_no = OVSP_LOCAL;
  1180. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1181. ovs_dp_change(dp, a);
  1182. /* So far only local changes have been made, now need the lock. */
  1183. ovs_lock();
  1184. vport = new_vport(&parms);
  1185. if (IS_ERR(vport)) {
  1186. err = PTR_ERR(vport);
  1187. if (err == -EBUSY)
  1188. err = -EEXIST;
  1189. if (err == -EEXIST) {
  1190. /* An outdated user space instance that does not understand
  1191. * the concept of user_features has attempted to create a new
  1192. * datapath and is likely to reuse it. Drop all user features.
  1193. */
  1194. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1195. ovs_dp_reset_user_features(skb, info);
  1196. }
  1197. goto err_destroy_ports_array;
  1198. }
  1199. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1200. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1201. BUG_ON(err < 0);
  1202. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1203. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1204. ovs_unlock();
  1205. ovs_notify(&dp_datapath_genl_family, reply, info);
  1206. return 0;
  1207. err_destroy_ports_array:
  1208. ovs_unlock();
  1209. kfree(dp->ports);
  1210. err_destroy_percpu:
  1211. free_percpu(dp->stats_percpu);
  1212. err_destroy_table:
  1213. ovs_flow_tbl_destroy(&dp->table, false);
  1214. err_free_dp:
  1215. release_net(ovs_dp_get_net(dp));
  1216. kfree(dp);
  1217. err_free_reply:
  1218. kfree_skb(reply);
  1219. err:
  1220. return err;
  1221. }
  1222. /* Called with ovs_mutex. */
  1223. static void __dp_destroy(struct datapath *dp)
  1224. {
  1225. int i;
  1226. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1227. struct vport *vport;
  1228. struct hlist_node *n;
  1229. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1230. if (vport->port_no != OVSP_LOCAL)
  1231. ovs_dp_detach_port(vport);
  1232. }
  1233. list_del_rcu(&dp->list_node);
  1234. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1235. * all ports in datapath are destroyed first before freeing datapath.
  1236. */
  1237. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1238. /* RCU destroy the flow table */
  1239. ovs_flow_tbl_destroy(&dp->table, true);
  1240. call_rcu(&dp->rcu, destroy_dp_rcu);
  1241. }
  1242. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1243. {
  1244. struct sk_buff *reply;
  1245. struct datapath *dp;
  1246. int err;
  1247. reply = ovs_dp_cmd_alloc_info(info);
  1248. if (!reply)
  1249. return -ENOMEM;
  1250. ovs_lock();
  1251. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1252. err = PTR_ERR(dp);
  1253. if (IS_ERR(dp))
  1254. goto err_unlock_free;
  1255. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1256. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1257. BUG_ON(err < 0);
  1258. __dp_destroy(dp);
  1259. ovs_unlock();
  1260. ovs_notify(&dp_datapath_genl_family, reply, info);
  1261. return 0;
  1262. err_unlock_free:
  1263. ovs_unlock();
  1264. kfree_skb(reply);
  1265. return err;
  1266. }
  1267. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1268. {
  1269. struct sk_buff *reply;
  1270. struct datapath *dp;
  1271. int err;
  1272. reply = ovs_dp_cmd_alloc_info(info);
  1273. if (!reply)
  1274. return -ENOMEM;
  1275. ovs_lock();
  1276. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1277. err = PTR_ERR(dp);
  1278. if (IS_ERR(dp))
  1279. goto err_unlock_free;
  1280. ovs_dp_change(dp, info->attrs);
  1281. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1282. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1283. BUG_ON(err < 0);
  1284. ovs_unlock();
  1285. ovs_notify(&dp_datapath_genl_family, reply, info);
  1286. return 0;
  1287. err_unlock_free:
  1288. ovs_unlock();
  1289. kfree_skb(reply);
  1290. return err;
  1291. }
  1292. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1293. {
  1294. struct sk_buff *reply;
  1295. struct datapath *dp;
  1296. int err;
  1297. reply = ovs_dp_cmd_alloc_info(info);
  1298. if (!reply)
  1299. return -ENOMEM;
  1300. ovs_lock();
  1301. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1302. if (IS_ERR(dp)) {
  1303. err = PTR_ERR(dp);
  1304. goto err_unlock_free;
  1305. }
  1306. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1307. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1308. BUG_ON(err < 0);
  1309. ovs_unlock();
  1310. return genlmsg_reply(reply, info);
  1311. err_unlock_free:
  1312. ovs_unlock();
  1313. kfree_skb(reply);
  1314. return err;
  1315. }
  1316. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1317. {
  1318. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1319. struct datapath *dp;
  1320. int skip = cb->args[0];
  1321. int i = 0;
  1322. ovs_lock();
  1323. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1324. if (i >= skip &&
  1325. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1326. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1327. OVS_DP_CMD_NEW) < 0)
  1328. break;
  1329. i++;
  1330. }
  1331. ovs_unlock();
  1332. cb->args[0] = i;
  1333. return skb->len;
  1334. }
  1335. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1336. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1337. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1338. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1339. };
  1340. static const struct genl_ops dp_datapath_genl_ops[] = {
  1341. { .cmd = OVS_DP_CMD_NEW,
  1342. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1343. .policy = datapath_policy,
  1344. .doit = ovs_dp_cmd_new
  1345. },
  1346. { .cmd = OVS_DP_CMD_DEL,
  1347. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1348. .policy = datapath_policy,
  1349. .doit = ovs_dp_cmd_del
  1350. },
  1351. { .cmd = OVS_DP_CMD_GET,
  1352. .flags = 0, /* OK for unprivileged users. */
  1353. .policy = datapath_policy,
  1354. .doit = ovs_dp_cmd_get,
  1355. .dumpit = ovs_dp_cmd_dump
  1356. },
  1357. { .cmd = OVS_DP_CMD_SET,
  1358. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1359. .policy = datapath_policy,
  1360. .doit = ovs_dp_cmd_set,
  1361. },
  1362. };
  1363. static struct genl_family dp_datapath_genl_family = {
  1364. .id = GENL_ID_GENERATE,
  1365. .hdrsize = sizeof(struct ovs_header),
  1366. .name = OVS_DATAPATH_FAMILY,
  1367. .version = OVS_DATAPATH_VERSION,
  1368. .maxattr = OVS_DP_ATTR_MAX,
  1369. .netnsok = true,
  1370. .parallel_ops = true,
  1371. .ops = dp_datapath_genl_ops,
  1372. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1373. .mcgrps = &ovs_dp_datapath_multicast_group,
  1374. .n_mcgrps = 1,
  1375. };
  1376. /* Called with ovs_mutex or RCU read lock. */
  1377. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1378. u32 portid, u32 seq, u32 flags, u8 cmd)
  1379. {
  1380. struct ovs_header *ovs_header;
  1381. struct ovs_vport_stats vport_stats;
  1382. int err;
  1383. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1384. flags, cmd);
  1385. if (!ovs_header)
  1386. return -EMSGSIZE;
  1387. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1388. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1389. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1390. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1391. vport->ops->get_name(vport)))
  1392. goto nla_put_failure;
  1393. ovs_vport_get_stats(vport, &vport_stats);
  1394. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1395. &vport_stats))
  1396. goto nla_put_failure;
  1397. if (ovs_vport_get_upcall_portids(vport, skb))
  1398. goto nla_put_failure;
  1399. err = ovs_vport_get_options(vport, skb);
  1400. if (err == -EMSGSIZE)
  1401. goto error;
  1402. return genlmsg_end(skb, ovs_header);
  1403. nla_put_failure:
  1404. err = -EMSGSIZE;
  1405. error:
  1406. genlmsg_cancel(skb, ovs_header);
  1407. return err;
  1408. }
  1409. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1410. {
  1411. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1412. }
  1413. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1414. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1415. u32 seq, u8 cmd)
  1416. {
  1417. struct sk_buff *skb;
  1418. int retval;
  1419. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1420. if (!skb)
  1421. return ERR_PTR(-ENOMEM);
  1422. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1423. BUG_ON(retval < 0);
  1424. return skb;
  1425. }
  1426. /* Called with ovs_mutex or RCU read lock. */
  1427. static struct vport *lookup_vport(struct net *net,
  1428. struct ovs_header *ovs_header,
  1429. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1430. {
  1431. struct datapath *dp;
  1432. struct vport *vport;
  1433. if (a[OVS_VPORT_ATTR_NAME]) {
  1434. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1435. if (!vport)
  1436. return ERR_PTR(-ENODEV);
  1437. if (ovs_header->dp_ifindex &&
  1438. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1439. return ERR_PTR(-ENODEV);
  1440. return vport;
  1441. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1442. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1443. if (port_no >= DP_MAX_PORTS)
  1444. return ERR_PTR(-EFBIG);
  1445. dp = get_dp(net, ovs_header->dp_ifindex);
  1446. if (!dp)
  1447. return ERR_PTR(-ENODEV);
  1448. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1449. if (!vport)
  1450. return ERR_PTR(-ENODEV);
  1451. return vport;
  1452. } else
  1453. return ERR_PTR(-EINVAL);
  1454. }
  1455. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1456. {
  1457. struct nlattr **a = info->attrs;
  1458. struct ovs_header *ovs_header = info->userhdr;
  1459. struct vport_parms parms;
  1460. struct sk_buff *reply;
  1461. struct vport *vport;
  1462. struct datapath *dp;
  1463. u32 port_no;
  1464. int err;
  1465. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1466. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1467. return -EINVAL;
  1468. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1469. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1470. if (port_no >= DP_MAX_PORTS)
  1471. return -EFBIG;
  1472. reply = ovs_vport_cmd_alloc_info();
  1473. if (!reply)
  1474. return -ENOMEM;
  1475. ovs_lock();
  1476. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1477. err = -ENODEV;
  1478. if (!dp)
  1479. goto exit_unlock_free;
  1480. if (port_no) {
  1481. vport = ovs_vport_ovsl(dp, port_no);
  1482. err = -EBUSY;
  1483. if (vport)
  1484. goto exit_unlock_free;
  1485. } else {
  1486. for (port_no = 1; ; port_no++) {
  1487. if (port_no >= DP_MAX_PORTS) {
  1488. err = -EFBIG;
  1489. goto exit_unlock_free;
  1490. }
  1491. vport = ovs_vport_ovsl(dp, port_no);
  1492. if (!vport)
  1493. break;
  1494. }
  1495. }
  1496. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1497. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1498. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1499. parms.dp = dp;
  1500. parms.port_no = port_no;
  1501. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1502. vport = new_vport(&parms);
  1503. err = PTR_ERR(vport);
  1504. if (IS_ERR(vport))
  1505. goto exit_unlock_free;
  1506. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1507. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1508. BUG_ON(err < 0);
  1509. ovs_unlock();
  1510. ovs_notify(&dp_vport_genl_family, reply, info);
  1511. return 0;
  1512. exit_unlock_free:
  1513. ovs_unlock();
  1514. kfree_skb(reply);
  1515. return err;
  1516. }
  1517. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1518. {
  1519. struct nlattr **a = info->attrs;
  1520. struct sk_buff *reply;
  1521. struct vport *vport;
  1522. int err;
  1523. reply = ovs_vport_cmd_alloc_info();
  1524. if (!reply)
  1525. return -ENOMEM;
  1526. ovs_lock();
  1527. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1528. err = PTR_ERR(vport);
  1529. if (IS_ERR(vport))
  1530. goto exit_unlock_free;
  1531. if (a[OVS_VPORT_ATTR_TYPE] &&
  1532. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1533. err = -EINVAL;
  1534. goto exit_unlock_free;
  1535. }
  1536. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1537. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1538. if (err)
  1539. goto exit_unlock_free;
  1540. }
  1541. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1542. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1543. err = ovs_vport_set_upcall_portids(vport, ids);
  1544. if (err)
  1545. goto exit_unlock_free;
  1546. }
  1547. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1548. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1549. BUG_ON(err < 0);
  1550. ovs_unlock();
  1551. ovs_notify(&dp_vport_genl_family, reply, info);
  1552. return 0;
  1553. exit_unlock_free:
  1554. ovs_unlock();
  1555. kfree_skb(reply);
  1556. return err;
  1557. }
  1558. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1559. {
  1560. struct nlattr **a = info->attrs;
  1561. struct sk_buff *reply;
  1562. struct vport *vport;
  1563. int err;
  1564. reply = ovs_vport_cmd_alloc_info();
  1565. if (!reply)
  1566. return -ENOMEM;
  1567. ovs_lock();
  1568. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1569. err = PTR_ERR(vport);
  1570. if (IS_ERR(vport))
  1571. goto exit_unlock_free;
  1572. if (vport->port_no == OVSP_LOCAL) {
  1573. err = -EINVAL;
  1574. goto exit_unlock_free;
  1575. }
  1576. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1577. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1578. BUG_ON(err < 0);
  1579. ovs_dp_detach_port(vport);
  1580. ovs_unlock();
  1581. ovs_notify(&dp_vport_genl_family, reply, info);
  1582. return 0;
  1583. exit_unlock_free:
  1584. ovs_unlock();
  1585. kfree_skb(reply);
  1586. return err;
  1587. }
  1588. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1589. {
  1590. struct nlattr **a = info->attrs;
  1591. struct ovs_header *ovs_header = info->userhdr;
  1592. struct sk_buff *reply;
  1593. struct vport *vport;
  1594. int err;
  1595. reply = ovs_vport_cmd_alloc_info();
  1596. if (!reply)
  1597. return -ENOMEM;
  1598. rcu_read_lock();
  1599. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1600. err = PTR_ERR(vport);
  1601. if (IS_ERR(vport))
  1602. goto exit_unlock_free;
  1603. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1604. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1605. BUG_ON(err < 0);
  1606. rcu_read_unlock();
  1607. return genlmsg_reply(reply, info);
  1608. exit_unlock_free:
  1609. rcu_read_unlock();
  1610. kfree_skb(reply);
  1611. return err;
  1612. }
  1613. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1614. {
  1615. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1616. struct datapath *dp;
  1617. int bucket = cb->args[0], skip = cb->args[1];
  1618. int i, j = 0;
  1619. rcu_read_lock();
  1620. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1621. if (!dp) {
  1622. rcu_read_unlock();
  1623. return -ENODEV;
  1624. }
  1625. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1626. struct vport *vport;
  1627. j = 0;
  1628. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1629. if (j >= skip &&
  1630. ovs_vport_cmd_fill_info(vport, skb,
  1631. NETLINK_CB(cb->skb).portid,
  1632. cb->nlh->nlmsg_seq,
  1633. NLM_F_MULTI,
  1634. OVS_VPORT_CMD_NEW) < 0)
  1635. goto out;
  1636. j++;
  1637. }
  1638. skip = 0;
  1639. }
  1640. out:
  1641. rcu_read_unlock();
  1642. cb->args[0] = i;
  1643. cb->args[1] = j;
  1644. return skb->len;
  1645. }
  1646. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1647. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1648. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1649. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1650. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1651. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1652. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1653. };
  1654. static const struct genl_ops dp_vport_genl_ops[] = {
  1655. { .cmd = OVS_VPORT_CMD_NEW,
  1656. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1657. .policy = vport_policy,
  1658. .doit = ovs_vport_cmd_new
  1659. },
  1660. { .cmd = OVS_VPORT_CMD_DEL,
  1661. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1662. .policy = vport_policy,
  1663. .doit = ovs_vport_cmd_del
  1664. },
  1665. { .cmd = OVS_VPORT_CMD_GET,
  1666. .flags = 0, /* OK for unprivileged users. */
  1667. .policy = vport_policy,
  1668. .doit = ovs_vport_cmd_get,
  1669. .dumpit = ovs_vport_cmd_dump
  1670. },
  1671. { .cmd = OVS_VPORT_CMD_SET,
  1672. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1673. .policy = vport_policy,
  1674. .doit = ovs_vport_cmd_set,
  1675. },
  1676. };
  1677. struct genl_family dp_vport_genl_family = {
  1678. .id = GENL_ID_GENERATE,
  1679. .hdrsize = sizeof(struct ovs_header),
  1680. .name = OVS_VPORT_FAMILY,
  1681. .version = OVS_VPORT_VERSION,
  1682. .maxattr = OVS_VPORT_ATTR_MAX,
  1683. .netnsok = true,
  1684. .parallel_ops = true,
  1685. .ops = dp_vport_genl_ops,
  1686. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1687. .mcgrps = &ovs_dp_vport_multicast_group,
  1688. .n_mcgrps = 1,
  1689. };
  1690. static struct genl_family * const dp_genl_families[] = {
  1691. &dp_datapath_genl_family,
  1692. &dp_vport_genl_family,
  1693. &dp_flow_genl_family,
  1694. &dp_packet_genl_family,
  1695. };
  1696. static void dp_unregister_genl(int n_families)
  1697. {
  1698. int i;
  1699. for (i = 0; i < n_families; i++)
  1700. genl_unregister_family(dp_genl_families[i]);
  1701. }
  1702. static int dp_register_genl(void)
  1703. {
  1704. int err;
  1705. int i;
  1706. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1707. err = genl_register_family(dp_genl_families[i]);
  1708. if (err)
  1709. goto error;
  1710. }
  1711. return 0;
  1712. error:
  1713. dp_unregister_genl(i);
  1714. return err;
  1715. }
  1716. static int __net_init ovs_init_net(struct net *net)
  1717. {
  1718. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1719. INIT_LIST_HEAD(&ovs_net->dps);
  1720. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1721. return 0;
  1722. }
  1723. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1724. struct list_head *head)
  1725. {
  1726. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1727. struct datapath *dp;
  1728. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1729. int i;
  1730. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1731. struct vport *vport;
  1732. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1733. struct netdev_vport *netdev_vport;
  1734. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1735. continue;
  1736. netdev_vport = netdev_vport_priv(vport);
  1737. if (dev_net(netdev_vport->dev) == dnet)
  1738. list_add(&vport->detach_list, head);
  1739. }
  1740. }
  1741. }
  1742. }
  1743. static void __net_exit ovs_exit_net(struct net *dnet)
  1744. {
  1745. struct datapath *dp, *dp_next;
  1746. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1747. struct vport *vport, *vport_next;
  1748. struct net *net;
  1749. LIST_HEAD(head);
  1750. ovs_lock();
  1751. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1752. __dp_destroy(dp);
  1753. rtnl_lock();
  1754. for_each_net(net)
  1755. list_vports_from_net(net, dnet, &head);
  1756. rtnl_unlock();
  1757. /* Detach all vports from given namespace. */
  1758. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1759. list_del(&vport->detach_list);
  1760. ovs_dp_detach_port(vport);
  1761. }
  1762. ovs_unlock();
  1763. cancel_work_sync(&ovs_net->dp_notify_work);
  1764. }
  1765. static struct pernet_operations ovs_net_ops = {
  1766. .init = ovs_init_net,
  1767. .exit = ovs_exit_net,
  1768. .id = &ovs_net_id,
  1769. .size = sizeof(struct ovs_net),
  1770. };
  1771. static int __init dp_init(void)
  1772. {
  1773. int err;
  1774. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1775. pr_info("Open vSwitch switching datapath\n");
  1776. err = action_fifos_init();
  1777. if (err)
  1778. goto error;
  1779. err = ovs_internal_dev_rtnl_link_register();
  1780. if (err)
  1781. goto error_action_fifos_exit;
  1782. err = ovs_flow_init();
  1783. if (err)
  1784. goto error_unreg_rtnl_link;
  1785. err = ovs_vport_init();
  1786. if (err)
  1787. goto error_flow_exit;
  1788. err = register_pernet_device(&ovs_net_ops);
  1789. if (err)
  1790. goto error_vport_exit;
  1791. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1792. if (err)
  1793. goto error_netns_exit;
  1794. err = dp_register_genl();
  1795. if (err < 0)
  1796. goto error_unreg_notifier;
  1797. return 0;
  1798. error_unreg_notifier:
  1799. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1800. error_netns_exit:
  1801. unregister_pernet_device(&ovs_net_ops);
  1802. error_vport_exit:
  1803. ovs_vport_exit();
  1804. error_flow_exit:
  1805. ovs_flow_exit();
  1806. error_unreg_rtnl_link:
  1807. ovs_internal_dev_rtnl_link_unregister();
  1808. error_action_fifos_exit:
  1809. action_fifos_exit();
  1810. error:
  1811. return err;
  1812. }
  1813. static void dp_cleanup(void)
  1814. {
  1815. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1816. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1817. unregister_pernet_device(&ovs_net_ops);
  1818. rcu_barrier();
  1819. ovs_vport_exit();
  1820. ovs_flow_exit();
  1821. ovs_internal_dev_rtnl_link_unregister();
  1822. action_fifos_exit();
  1823. }
  1824. module_init(dp_init);
  1825. module_exit(dp_cleanup);
  1826. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1827. MODULE_LICENSE("GPL");