datapath.c 47 KB

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